import axios from 'axios'
import { Message } from 'element-ui'

// create an axios instance
const service = axios.create({
  // baseURL: process.env.BASE_API, // api的base_url
  timeout: 5000 // request timeout
})

// request interceptor
// service.interceptors.request.use(config => {
//   // Do something before request is sent
//   if (store.getters.accessToken) {
//     config.headers['X-SSAS-AccessToken'] = store.getters.accessToken
//   }
//   if (store.getters.refreshToken) {
//     config.headers['X-SSAS-RefreshToken'] = store.getters.refreshToken
//   }
//   return config
// }, error => {
//   // Do something with request error
//   console.log(error) // for debug
//   Promise.reject(error)
// })

// respone interceptor
service.interceptors.response.use(
  // response => {
  //   const newToken = response.headers['x-ssas-accesstokenreset']
  //   if (newToken) {
  //     store.commit('SET_ACCESS_TOKEN', newToken)
  //   }
  //   return response
  // },
  response => response,
  error => {
    let message = ''
    switch (error.response.status) {
      case 500: message = '服务器开小差，发生未知内部错误...'; break
      case 400: message = '后台小哥很无辜，请不要干坏事!'; break
      case 401: message = '你还没有登录或已登录失效'; break
      default: message = '得到一个未知错误响应'
    }
    Message({
      message,
      type: 'error',
      duration: 3 * 1000
    })
    return Promise.reject(error.response)
  })

export default service
